/**
 * Project oo_thread
 */


#include "Thread.h"
#include <stdio.h>

/**
 * Thread implementation
 */


Thread::Thread()
:_pthid(0)
,_isRunning(false)
{


}

/**
 * @return void
 */
void Thread::start() {
    //创建线程，并且将状态设置成true
    int ret = pthread_create(&_pthid, NULL, start_routine, this);
    if(ret != 0)
    {
        //创建线程失败
        fprintf(stderr, "创建线程失败！\n");
        return;
    }
    _isRunning = true;
}

/**
 * @return void
 */
void Thread::join() {
    if(_isRunning)
    {
        pthread_join(_pthid,NULL);
    }
}

/**
 * @param void *
 * @return void *
 */
void * Thread::start_routine(void * arg) {
    //执行线程入口函数
    Thread *th = static_cast<Thread*>(arg);
    //多态
    if(th)
        th->run();
    return nullptr;
    
}

